<template>
  <section>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters" ref="formRef">
        <el-form-item label="用户姓名">
          <el-input v-model="filters.keyword" placeholder="请输入用户姓名"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱">
          <el-input v-model="filters.email" placeholder="请输入用户邮箱"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button type="danger" @click="resetForm('formRef')">重置</el-button>
          <el-button type="primary" @click="addData">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="tableData"
              highlight-current-row
              @selection-change="selsChange"
              border
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号">
      </el-table-column>
      <el-table-column prop="headImg" label="头像" width="100" align="center">
        <template slot-scope= "scope">
          <img :src="scope.row.headImg"  style= "width: 50px;height:50px">
        </template>
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="90">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="190">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="190">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="190">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="190">
      </el-table-column>
      <el-table-column prop="updateTime" label="编辑时间" width="190">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80">
        <!-- 如果需要对表中某个字段进行显示内容的切换，则可以使用<template>语法进行转换 -->
        <!-- 分析：如果是0正常；如果是1：禁用-->
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
          <el-tag size="mini" v-if="scope.row.state == 1" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--弹框界面-->
    <el-dialog :title="titleValue" :visible.sync="dialogTableVisible">
      <el-form :model="addForm" label-width="100px" :rules="formRules" ref="addForm">

        <el-form-item label="头像" prop="headImg">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/file/fileUpload"
              name="fileName"
              :on-success="handleSuccess"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              :limit=1
              accept=".jpg,.png"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过1MB</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input type="text"  v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="addForm.sex">
            <el-radio class="radio" :label="1">男</el-radio>
            <el-radio class="radio" :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email" ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addForm.phone" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="1">正常</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dialogTableVisible = false">取消</el-button>
        <el-button type="primary" @click.native="onSubmit">提交</el-button>
      </div>
    </el-dialog>


    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--
        @size-change：选择了每页条数 ，触发handleSizeChange方法
        @current-change：选择了第几页 ，触发handleCurrentChange方法
        :@current-change：把当前页绑定到  current 变量
        :page-size="size" ：每页显示的条数
        :layout  ：分页条布局
        :total="tableTotal" ：总条数
     -->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="filters.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="filters.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
import {batchRemoveUser} from "../../api/api";

export default {
  name: 'Department',
  data() {
    return {
      titleValue: '',
      filters:{
        keyword: '',
        current: 1, //默认第几页
        size: 10, //默认每页显示条数
      },
      fileList:[{
        name:'',
        url:''
      }],
      tableData: [],
      optionUserData: [], //下拉部门选项集合
      optionDeptData:[],
      total: 0,//列表总数量
      dialogTableVisible: false, //表单弹框是否展示
      sels: [],//列表选中列数据
      formRules: { //表单校验规则
        name: [
          // { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      addForm:{},//表单对象
    }
  },
  //定义所有的方法
  methods: {
    //点击复选框时触发
    selsChange(sels) {
      this.sels = sels;
    },
    //重置表单
    resetForm() {
      this.filters = {};
    },
    //点击编辑按钮触发
    handleEdit(index, row){
      this.titleValue = '编辑员工';
      //显示弹框
      this.dialogTableVisible = true;
      this.addForm = Object.assign({}, row);//属性拷贝
    },
    //提交表单
    onSubmit(){
      if(this.addForm.id){
        //id有值，做修改
        //发请求
        this.$http.post("/user/update", this.addForm).then(res=>{
          //隐藏弹框
          this.dialogTableVisible = false;
          //刷新列表
          this.getTableData();
          //提示信息
          if(res.data.success){
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'success'
            });
          }else{
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          console.log(error);
        });
      }else{
        //id没有值，做新增操作
        //发请求
        this.$http.post("/user/add", this.addForm).then(res=>{
          //隐藏弹框
          this.dialogTableVisible = false;
          //刷新列表
          this.getTableData();
          //提示信息
          if(res.data.success){
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'success'
            });
          }else{
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          console.log(error);
        });
      }
    },
    //点击新增
    addData(){
      //显示弹框
      this.dialogTableVisible = true;
      this.titleValue = '新增员工';
      //清空表单数据
      this.addForm = {};
    },
    //删除
    handleDelete(index, row){
      this.$confirm('确定删除此记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //点击确定执行下面代码
        //发请求
        this.$http.get("/user/delete/" + row.id, null).then(res=>{
          this.getTableData();//刷新列表数据
          if(res.data.success){
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'success'
            });
          }else{
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          console.log(error);
        });
      }).catch(error => {
        console.log(error);
      });
    },
    //批量删除
    batchRemove() {
      //从sels中获取id值形成集合，传给后台做批量删除
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        let param = { ids: ids };
        //发请求
        this.$http.post("/user/batchDelete", param).then(res=>{
          this.getTableData();//刷新列表数据
          if(res.data.success){
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'success'
            });
          }else{
            this.$notify({
              title: '提示',
              message: res.data.msg,
              type: 'error'
            });
          }
        }).catch(error => {
          console.log(error);
        });
      }).catch(error => {
        console.log(error);
      });
    },
    //获取列表数据
    getTableData(){
      //发请求
      this.$http.post("/user/query", this.filters).then(res=>{
        console.log("111")
        console.log(res)
        if(res.data.success == true){
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        }
      }).catch(error => {
        console.log(error);
      });
    },
    //选择每页条数时触发
    handleSizeChange(val){
      console.log('选择了每页条数=' + val);
      this.filters.size = val;
      this.getTableData();
    },
    //翻页触发该函数
    handleCurrentChange(val){
      console.log('翻页了=' + val);
      this.filters.current = val;
      this.getTableData();
    },
    //提供一个查询方法：查询部门表中所有的部门列表，然后给前端进行【上级部门的查询显示】
    findAllUser(){
      this.$http.post('/user/all').then(res=>{
        this.optionUserData = res.data.data;
      }).catch(error => {
        console.log(error);
      });
    },
    //图片上传成功
    handleSuccess(response){

      if (response.success){
        this.addForm.headImg=response.data;
      }else {
        this.$message({
          message: response.message,
          type: 'error'
        });
      }
    },
//移除文件上传
    handleRemove() {

      let quanfile={
        url:this.addForm.headImg,
      }
      this.$http.post("/file/fileDel",quanfile).then(res=>{

        if(res.data.success){
          this.$message({
            message: res.data.message,
            type: 'success'
          });
        }

      });

    },
//预览
    handlePreview(file) {
      console.log("------------------------")
      console.log(file);
    },
  },

  //钩子函数：数据初始化
  mounted() {
    this.getTableData();
    //查询所有用户信息
    this.findAllUser();
  }
}
</script>

<style scoped>
</style>